package com.lxl.bt.util;

import android.graphics.drawable.Drawable;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.lxl.bt.R;
import com.lxl.bt.bean.AlertCallback;

import jp.wasabeef.glide.transformations.GrayscaleTransformation;


/**
 * Created by songp on 2018/1/31.
 */

public class GlideUtils {


    /*
     *加载图片(默认)
     */
    public static void loadImage(ImageView imageView, String url) {
        RequestOptions options = new RequestOptions()
                .centerCrop()
                .placeholder(R.mipmap.noimage) //占位图
                .error(R.mipmap.noimage)       //错误图
                // .priority(Priority.HIGH)
                .diskCacheStrategy(DiskCacheStrategy.ALL);
        Glide.with(imageView.getContext()).load(url).apply(options).into(imageView);
    }
    /*
     *加载图片(默认)
     */
    public static void loadImage(ImageView imageView, String url,int res) {
        RequestOptions options = new RequestOptions()
                .centerCrop()
                .placeholder(res) //占位图
                .error(res)       //错误图
                // .priority(Priority.HIGH)
                .diskCacheStrategy(DiskCacheStrategy.ALL);
        Glide.with(imageView.getContext()).load(url).apply(options).into(imageView);
    }

    public static void onlyLoadImage(ImageView imageView, String url) {
        RequestOptions options = new RequestOptions()
                .placeholder(R.mipmap.noimage) //占位图
                .error(R.mipmap.noimage)       //错误图
                // .priority(Priority.HIGH)
                .diskCacheStrategy(DiskCacheStrategy.ALL);
        Glide.with(imageView.getContext()).load(url).apply(options).into(imageView);
    }

    /*
     *加载图片(默认)
     */
    public static void loadImage(ImageView imageView, String url, AlertCallback alertCallback) {
        RequestOptions options = new RequestOptions()
                .centerCrop()
                .placeholder(R.mipmap.noimage) //占位图
                .error(R.mipmap.noimage)       //错误图
                // .priority(Priority.HIGH)

                .diskCacheStrategy(DiskCacheStrategy.ALL);


        Glide.with(imageView.getContext()).load(url).into(new SimpleTarget<Drawable>() {
            @Override
            public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
                imageView.setImageDrawable(resource);
                alertCallback.doConfirm(resource);
            }
        });
    }

    public static void loadImage(ImageView imageView, Object object, int errRes) {
        RequestOptions options = new RequestOptions()
//                .centerCrop()
                .placeholder(errRes) //占位图
                .error(errRes)       //错误图
                // .priority(Priority.HIGH)
                .diskCacheStrategy(DiskCacheStrategy.ALL);
//        if(url instanceof  String){
        Glide.with(imageView.getContext()).load(object).apply(options).into(imageView);
//        }else {
//            Glide.with(imageView.getContext()).load((Drawable) url).apply(options).into(imageView);
//        }

    }


    /*
     *加载圆形图片
     */
    public static void loadCircleImage(ImageView imageView, String url) {
        RequestOptions options = new RequestOptions()
                .centerCrop()
                .circleCrop()//设置圆形
                .placeholder(R.mipmap.noimage)
                .error(R.mipmap.noimage)
                //.priority(Priority.HIGH)
                .diskCacheStrategy(DiskCacheStrategy.ALL);
        Glide.with(imageView.getContext()).load(url).apply(options).into(imageView);

    }

    /*
     *加载圆形图片
     */
    public static void loadCircleImage(ImageView imageView, Drawable drawable) {
        RequestOptions options = new RequestOptions()
                .centerCrop()
                .circleCrop()//设置圆形
                .placeholder(drawable)
                .error(drawable)
                //.priority(Priority.HIGH)
                .diskCacheStrategy(DiskCacheStrategy.ALL);
        Glide.with(imageView.getContext()).load(drawable).apply(options).into(imageView);

    }

    /*
     *加载灰度(黑白)图片（自定义透明度）
     */
    public static void loadBlackImage(ImageView imageView, String url) {
        RequestOptions options = new RequestOptions()
                .centerCrop()
                .placeholder(R.mipmap.noimage)
                .error(R.mipmap.noimage)
                //.priority(Priority.HIGH)
                .bitmapTransform(new GrayscaleTransformation())
                .diskCacheStrategy(DiskCacheStrategy.ALL);
        Glide.with(imageView.getContext()).load(url).apply(options).into(imageView);
    }

    /*
     *加载圆角图片
     */
    public static void loadRadius(ImageView imageView, String url, int radius) {
        //设置图片圆角角度
        RoundedCorners roundedCorners = new RoundedCorners(radius);
//通过RequestOptions扩展功能,override:采样率,因为ImageView就这么大,可以压缩图片,降低内存消耗
        RequestOptions options = RequestOptions.bitmapTransform(roundedCorners);
        options.placeholder(R.mipmap.noimage) //占位图
                .error(R.mipmap.noimage).override(100,100)  ;     //错误图
        Glide.with(imageView.getContext()).load(url).apply(options).into(imageView);
    }
    /*
     *加载圆角图片
     */
    public static void loadRadius(ImageView imageView, String url, int radius,int defres) {
        //设置图片圆角角度
        RoundedCorners roundedCorners = new RoundedCorners(radius);
//通过RequestOptions扩展功能,override:采样率,因为ImageView就这么大,可以压缩图片,降低内存消耗
        RequestOptions options = RequestOptions.bitmapTransform(roundedCorners);
        options.placeholder(defres) //占位图
                .error(defres)  ;     //错误图
        Glide.with(imageView.getContext()).load(url).apply(options).into(imageView);
    }
    /*
     *加载圆角图片
     */
    public static void loadRadius(ImageView imageView, int res, int radius) {
        //设置图片圆角角度
        RoundedCorners roundedCorners = new RoundedCorners(radius);
//通过RequestOptions扩展功能,override:采样率,因为ImageView就这么大,可以压缩图片,降低内存消耗
        RequestOptions options = RequestOptions.bitmapTransform(roundedCorners);
        options.placeholder(R.mipmap.noimage) //占位图
                .error(R.mipmap.noimage).override(100,100) ;     //错误图
        Glide.with(imageView.getContext()).load(res).apply(options).into(imageView);
    }
}
